package com.yc.sole.system.controller;

import com.yc.sole.framework.mybatis.page.BaseBatchIdsReq;
import com.yc.sole.framework.mybatis.page.BasePageReq;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.result.Result;
import com.yc.sole.framework.validate.DeleteGroup;
import com.yc.sole.framework.validate.SaveGroup;
import com.yc.sole.framework.validate.UpdateGroup;
import com.yc.sole.framework.web.PreAuth;
import com.yc.sole.system.biz.DictBiz;
import com.yc.sole.system.request.DictQueryReq;
import com.yc.sole.system.request.DictReq;
import com.yc.sole.system.response.DictItemResp;
import com.yc.sole.system.response.DictResp;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 字典管理
 *
 * @author yizuomin
 * @date 2023-03-26
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
public class DictController {

    private final DictBiz dictBiz;

    /**
     * 新增
     *
     * @param dictReq
     * @return RestResult
     */
    @PreAuth(value = "system:dict:save", description = "新增字典")
    @PostMapping("/api/system/dict/saveDict")
    public Result<Void> saveDict(@RequestBody @Validated(SaveGroup.class) DictReq dictReq) {
        dictBiz.saveDict(dictReq);
        return Result.success();
    }

    /**
     * 通过Id修改
     *
     * @param dictReq
     * @return RestResult
     */
    @PreAuth(value = "system:dict:update", description = "修改字典")
    @PostMapping("/api/system/dict/updateDictById")
    public Result<Void> updateDictById(@RequestBody @Validated(UpdateGroup.class) DictReq dictReq) {
        dictBiz.updateDictById(dictReq);
        return Result.success();
    }

    /**
     * 通过Id查询字典
     *
     * @param dictId
     * @return RestResult
     */
    @PreAuth(value = "system:dict:select", description = "通过Id查询字典")
    @PostMapping("/api/system/dict/selectDictById/{dictId}")
    public Result<DictResp> selectDictById(@PathVariable Long dictId) {
        return Result.data(dictBiz.selectDictById(dictId));
    }

    /**
     * 通过id删除
     *
     * @param baseDeleteReq
     * @return RestResult
     */
    @PreAuth(value = "system:dict:delete", description = "删除字典")
    @PostMapping("/api/system/dict/deleteDict")
    public Result<Void> deleteDict(@RequestBody @Validated(DeleteGroup.class) BaseBatchIdsReq baseDeleteReq) {
        dictBiz.deleteDict(baseDeleteReq.getIds());
        return Result.success();
    }


    /**
     * 分页查询
     *
     * @param basePageReq
     * @return Result
     */
    @PreAuth(value = "system:dict:select", description = "分页查询字典")
    @PostMapping("/api/system/dict/selectDictPage")
    public Result<Paging<DictResp>> selectDictPage(@RequestBody @Validated BasePageReq<DictQueryReq> basePageReq) {
        return Result.data(dictBiz.selectDictPage(basePageReq.toPage(), basePageReq.getQuery()));
    }

    /**
     * 查询所有字典
     *
     * @return Result
     */
    @PreAuth(value = "system:dict:select", description = "查询所有字典")
    @PostMapping("/api/system/dict/selectAllDict")
    public Result<Map<String, List<DictItemResp>>> selectAllDict() {
        return Result.data(dictBiz.selectAllDict());
    }

    /**
     * 根据字典编码查询字典项
     *
     * @return Result
     */
    @PreAuth(value = "system:dict:select", description = "根据字典编码查询字典项", check = false)
    @PostMapping("/api/system/dict/selectByDictKey/{dictKey}")
    public Result<List<DictItemResp>> selectByDictKey(@PathVariable String dictKey) {
        return Result.data(dictBiz.selectByDictKey(dictKey));
    }
}
